from pylib import models
from pylib import web

from django.utils import simplejson
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class SearchPersonAPI(web.BaseHandler):
  @web.retry
  def get(self):
    query = self.request.get('query').strip()
    results = []
    for identity in  models.Identity.all().search(query):
      results.append({
        'identity': str(identity.key()),
        'name': identity.name,
        'person': str(identity.person.key()),
      })
    self.response.headers['Content-Type'] = 'application/json'
    self.response.out.write(simplejson.dumps(results))


application = webapp.WSGIApplication([('.*', SearchPersonAPI)])

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
